#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QClipboard>

#include <QFileDialog>
#include <QFileDevice>
#include <QImage>
#include <QPixmap>
#include "image.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <stdio.h>

using namespace std;

using namespace cv;

Mat image;
Mat image_cv;
Mat image_hsv_cv;
Mat image_hsv_cut;
Mat image_cv_cut;
Mat image_cv_out;

QImage image_open;
QImage image_open_cut;

int Low1 = 0, Low2 = 0, Low3 = 0, High1 = 255, High2 = 255, High3 = 255;
bool Mode = true;
bool CutMode = false;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->actionHSV->setChecked(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_6_clicked()
{
    ui->spinBox1->setValue(0);
    ui->spinBox2->setValue(255);
    ui->spinBox3->setValue(0);
    ui->spinBox4->setValue(255);
    ui->spinBox5->setValue(0);
    ui->spinBox6->setValue(255);
}


void MainWindow::on_pushButton_5_clicked()
{
    QClipboard *clipboard = QApplication::clipboard();   //获取系统剪贴板指针
    clipboard->setText(ui->lineEdit->text());
}


void MainWindow::on_checkBoxCutImage_stateChanged(int arg1)
{
    CutMode = ui->checkBoxCutImage->isChecked();
}


void MainWindow::on_spinBox1_valueChanged(int arg1)
{
    Low1 = arg1;
    refresh();
}


void MainWindow::on_spinBox2_valueChanged(int arg1)
{
    High1 = arg1;
    refresh();
}


void MainWindow::on_spinBox3_valueChanged(int arg1)
{
    Low2 = arg1;
    refresh();
}


void MainWindow::on_spinBox4_valueChanged(int arg1)
{
    High2 = arg1;
    refresh();
}


void MainWindow::on_spinBox5_valueChanged(int arg1)
{
    Low3 = arg1;
    refresh();
}


void MainWindow::on_spinBox6_valueChanged(int arg1)
{
    High3 = arg1;
    refresh();
}


void MainWindow::on_actionopen_triggered()
{
    QFileDialog fileDialog;
    QString fileName = fileDialog.getOpenFileName(this, "Open Image", "", "Image File(*.jpg *.png)");
    if(fileName == "") {
        return;
    }
    image_open.load(fileName);
    image_open_cut = image_open.scaled(300, 300);
    image_cv = QImage2cvMat(image_open);
    cvtColor(image_cv, image_hsv_cv, COLOR_BGR2HSV);
    image_cv_cut = QImage2cvMat(image_open_cut);
    cvtColor(image_cv_cut, image_hsv_cut, COLOR_BGR2HSV);
    cvtColor(image_cv, image_cv, COLOR_BGR2RGB);
    cvtColor(image_cv_cut, image_cv_cut, COLOR_BGR2RGB);
    ui->ImageOrigin->setPixmap(QPixmap::fromImage(image_open_cut));
}


void MainWindow::on_actionsave_triggered()
{
    QFileDialog fileDialog;
    QString fileName = fileDialog.getSaveFileName(this, "Save Image", "", "Image File(*.jpg *.png)");
    imwrite(fileName.toStdString(), image_cv_out);
}


void MainWindow::on_actionHSV_triggered()
{
    ui->actionHSV->setChecked(true);
    ui->actionRGB->setChecked(false);
    Mode = true;
}


void MainWindow::on_actionRGB_triggered()
{
    ui->actionHSV->setChecked(false);
    ui->actionRGB->setChecked(true);
    Mode = false;
}


void MainWindow::refresh()
{
    QString text = "";
    if(High1 >= Low1) {
        text += "(" + QString::number(Low1) + "-" + QString::number(High1) + ")";
    } else {
        text += "(0-" + QString::number(High1) + "," + QString::number(Low1) + "-255)";
    }
    if(High2 >= Low2) {
        text += "(" + QString::number(Low2) + "-" + QString::number(High2) + ")";
    } else {
        text += "(0-" + QString::number(High2) + "," + QString::number(Low2) + "-255)";
    }
    if(High3 >= Low3) {
        text += "(" + QString::number(Low3) + "-" + QString::number(High3) + ")";
    } else {
        text += "(0-" + QString::number(High3) + "," + QString::number(Low3) + "-255)";
    }
    ui->lineEdit->setText(text);
    if(Mode) {
        if(CutMode) {
            image_cv_out = threshold_split(image_hsv_cut);
        } else {
            image_cv_out = threshold_split(image_hsv_cv);
        }
    } else {
        if(CutMode) {
            image_cv_out = threshold_split(image_cv_cut);
        } else {
            image_cv_out = threshold_split(image_cv);
        }
    }
    QImage imagebinaryshow = cvMat2QImage(image_cv_out);
    imagebinaryshow = imagebinaryshow.scaled(300, 300);
    ui->ImageBinary->setPixmap(QPixmap::fromImage(imagebinaryshow));
}




